package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.FruitPackingItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.FruitDateListItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.FruitItemResultListItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class FruitPackingThread extends Thread{
	
	public interface FruitPackingThreadCallback {
		public void OnFruitPackingCompleted(boolean isNetworkConntected, FruitPackingItem fruitPackingItem);
		public void OnFruitPackingError(ErrorItem errorItem);
	}
	
	private String url;
	private FruitPackingThreadCallback callback;
	private ArrayList<NameValuePair> post;
	private String resOffCd, resNo, resSeq;
	private Context context;
	private AppStatus appStatus;
	
	public FruitPackingThread(Context context,
			FruitPackingThreadCallback callback, String resOffCd, String resNo, String resSeq) {
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.resOffCd = resOffCd;
		this.resNo = resNo;
		this.resSeq = resSeq;
	}
	
	@Override
	public void run() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnFruitPackingCompleted(false, null);
			return;
		}
		
		JSONParser jParser = new JSONParser();

		url = Const.MAIN_URL;
		post = ParameterUtil.getFirstFruitPackingParam(resOffCd, resNo, resSeq);
		
		JSONObject json = jParser.getJSONFromUrl(url, post);
		Log.i("url","url = " + url + post);
		Log.i("json","json = " + json);
		if(json == null) {	// json 값이 null 일때
			callback.OnFruitPackingError(null);
			return;
		}

		try {		
			int result = json.getInt(Const.RESULT);
			Log.i("result","result = " + result);
			ArrayList<FruitPackingItem> arrFruitItem = new ArrayList<FruitPackingItem>();
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);
				appStatus.setArrFruitPackingItem(arrFruitItem);
				callback.OnFruitPackingError(errorItem);
				
			}else{	// result 값이 0이 아닐 경우
				FruitPackingItem fruitPackingItem = new FruitPackingItem();
				
				JSONArray dataItem = json.getJSONArray(FruitPackingItem.DATA);
				
				ArrayList<FruitDateListItem> arrFruitData = new ArrayList<FruitDateListItem>();
				for(int i = 0; i < dataItem.length(); i++) {
					JSONObject itemObject = dataItem.getJSONObject(i);
					FruitDateListItem quaItem = new FruitDateListItem();
					quaItem.setFullResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitDateListItem.FULLRESNO)));
					quaItem.setFullRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitDateListItem.FULLRECNO)));
					quaItem.setAllPrdlstCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitDateListItem.ALLPRDLSTCODE)));
					quaItem.setPrdlstNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitDateListItem.PRDLSTNM)));
					quaItem.setInsQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitDateListItem.INSQTY)));
					quaItem.setQunKor(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitDateListItem.QUNKOR)));
					quaItem.setOrgNatNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitDateListItem.ORGNATNM)));
					quaItem.setResultProgrsSt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitDateListItem.RESULTPROGRSST)));
					arrFruitData.add(quaItem);
				}
				
				fruitPackingItem.setData(arrFruitData);
				
				
				JSONArray itemsResultItem = json.getJSONArray(FruitPackingItem.ITEMS);
				Log.i("itemsResultItem", itemsResultItem.toString());
				ArrayList<FruitItemResultListItem> arrResultData = new ArrayList<FruitItemResultListItem>();
				for(int i = 0; i < dataItem.length(); i++) {
					JSONObject itemObject = dataItem.getJSONObject(i);
					FruitItemResultListItem quaItem = new FruitItemResultListItem();
					quaItem.setResOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitItemResultListItem.RESOFFCD)));
					quaItem.setResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitItemResultListItem.RESNO)));
					quaItem.setResSeq(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitItemResultListItem.RESSEQ)));
					quaItem.setExportAreaCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitItemResultListItem.EXPORTAREACD)));
					quaItem.setAppointYear(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitItemResultListItem.APPOINTYEAR)));
					quaItem.setKorNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitItemResultListItem.KORNM)));
					quaItem.setAppointQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, FruitItemResultListItem.APPOINTQTY)));
					arrResultData.add(quaItem);
				}
				fruitPackingItem.setItems(arrResultData);
				
				
				callback.OnFruitPackingCompleted(true, fruitPackingItem);
			}
		} catch (JSONException e) {
		}
	}
	
}
